/**
 * 工具条
 */

import React, { Component, Fragment } from 'react';

import { Popconfirm, Divider, Tooltip } from 'antd';

import { doActionHandler, isEmpty, getLocalMessage } from '../common';

import { getUIActionGroup } from '../uiConfig';


export default class UIActionBar extends Component {
	constructor(props) {
		super(props);
		
		let actionGroup = getUIActionGroup(props.actionGroupId);

		if(actionGroup === undefined){
			console.error('can not find action group by id : ' + props.actionGroupId);
		}

		let actionBeans = actionGroup.actionBeans.map((action) => {
			return {
				text: action.label,
				desc: action.desc,
				actionGroupId: props.actionGroupId,
				actionId: action.id,
				isVisible: action.visible,
				handler: action.handler,
				confirmInfo: isEmpty(action.confirmInfoKey) ? '' : getLocalMessage(action.confirmInfoKey, action.confirmInfoKey)
			};
		});

		this.actions = actionBeans;
	}

	handleAction = (cellAction, data) => {
		const { history, actionHandler, parent } = this.props;
		
		const actionEvent = {
			actionId: cellAction.actionId,
			action: cellAction.handler,
			component: parent != null ? parent : this,
			params: data
		};
		if (actionHandler !== undefined) {
			
			if (cellAction.handler.startsWith('/') && history) {
				history.push(cellAction.handler, data);
			}
			else {
				doActionHandler(actionEvent, actionHandler(actionEvent));
			}
		}
	}

	render() {

		const { actions } = this;

		const { data, dataSet,totalCount,loginUserId } = this.props;

		let actionsBar = [];

		let actionVisibleMap = null;

		if (this.props.actionVisibleMap !== undefined) {
			actionVisibleMap = this.props.actionVisibleMap(actions, data, dataSet, loginUserId);
		}

		actions.forEach( (cellAction, index) => {
			let cellActionLink = null;
			
			if (((cellAction.isVisible === undefined || cellAction.isVisible === null) && actionVisibleMap === null) || (actionVisibleMap !== null && actionVisibleMap[cellAction.actionId] === true) || (cellAction.isVisible !== null && (typeof cellAction.isVisible === "string") && eval(cellAction.isVisible) === true) || cellAction.isVisible === true) {
				
				if(actionsBar.length > 0 ){
					actionsBar.push(<Divider key={index} type="vertical" />);
				}

				if (isEmpty(cellAction.desc) === false) {
					cellActionLink = <Tooltip mouseLeaveDelay={0} title={cellAction.desc}>{isEmpty(cellAction.confirmInfo) ? <a onClick={(e) => { this.handleAction(cellAction, data)} }>{cellAction.text}</a> : <a>{cellAction.text}</a>}</Tooltip>
				}
				else {
					cellActionLink = <Fragment>{isEmpty(cellAction.confirmInfo) ? <a onClick={(e) => { this.handleAction( cellAction, data)}}>{cellAction.text}</a> : <a>{cellAction.text}</a>}</Fragment>
				}

				if (isEmpty(cellAction.confirmInfo) === false) {
					actionsBar.push(<Popconfirm key={cellAction.actionId} title={cellAction.confirmInfo} onConfirm={(e) => {this.handleAction(cellAction, data)} }>{cellActionLink}</Popconfirm>);
				}
				else {
					actionsBar.push(<Fragment key={cellAction.actionId}>{cellActionLink}</Fragment>);
				}

			}
		});

		return (
			<Fragment>{actionsBar}</Fragment>
		);
	}
}
